package com.live.dengdengtv.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.live.dengdengtv.BaseActivity;
import com.live.dengdengtv.R;
import com.live.dengdengtv.utils.DeviceParams;

public class InputView extends LinearLayout {
	public static final int TYPE_TEXT = 1;
	public static final int TYPE_SELECT = 2;
	public static final int TYPE_TIME = 3;
	public static final int TYPE_TOGGLE = 4;
	public static final int TYPE_BUTTON = 5;
	public static final int TYPE_TEXT_EXTEND = 6;

	protected TextView lable;
	protected CustomEditText input;
	protected SelectView select;
	protected ToggleButton toggleButton;
	protected int inputType;
	protected int color;
	private String verifyError;
	private boolean newActivityInput;
	private final int background = 0;// R.drawable.textfield;

	private boolean initFlag;// 初始化类型
	/**扩展功能按钮*/
	private Button extendButton;

	public InputView(Context context, int inputType) {
		super(context);
		this.initFlag = true;
		this.inputType = inputType;
		this.newActivityInput = true;
		this.init(context, null, null, R.style.input_lable, R.style.input_text,
				false, 1);

		this.setGravity(Gravity.CENTER_VERTICAL);
		float density = DeviceParams.density(context);
		int left = (int) (10 * density), top = (int) (5 * density);// 这个值必须和input_layout中相同
		int right = top, bottom = top;

		this.setPadding(left, top, right, bottom);

		switch (inputType) {
		case TYPE_TEXT:
			input.setPadding(left, top, right, bottom);
			break;
		case TYPE_TEXT_EXTEND:
			input.setPadding(left, top, right, bottom);
			break;
		case TYPE_SELECT:
			select.setPadding(left, top, right, bottom);
			break;
		case TYPE_TIME:
			break;
		default:
			break;
		}
	}
	
	public InputView(Context context, int inputType,String text) {
		super(context);
		this.initFlag = true;
		this.inputType = inputType;
		this.newActivityInput = true;
		this.init(context, null, text, R.style.input_lable, R.style.input_text,
				false, 1);

		this.setGravity(Gravity.CENTER_VERTICAL);
		float density = DeviceParams.density(context);
		int left = (int) (10 * density), top = (int) (5 * density);// 这个值必须和input_layout中相同
		int right = top, bottom = top;

		this.setPadding(left, top, right, bottom);

		switch (inputType) {
		case TYPE_TEXT:
			input.setPadding(left, top, right, bottom);
			break;
		case TYPE_TEXT_EXTEND:
			input.setPadding(left, top, right, bottom);
			break;
		case TYPE_SELECT:
			select.setPadding(left, top, right, bottom);
			break;
		case TYPE_TIME:
			break;
		default:
			break;
		}
	}
	
	public InputView(Context context, int inputType,String text,int color) {
		super(context);
		this.initFlag = true;
		this.inputType = inputType;
		this.newActivityInput = true;
		this.color = color;
		this.init(context, null, text, R.style.input_lable, R.style.input_text,
				false, 1);

		this.setGravity(Gravity.CENTER_VERTICAL);
		float density = DeviceParams.density(context);
		int left = (int) (10 * density), top = (int) (5 * density);// 这个值必须和input_layout中相同
		int right = top, bottom = top;

		this.setPadding(left, top, right, bottom);

		switch (inputType) {
		case TYPE_TEXT:
			input.setPadding(left, top, right, bottom);
			break;
		case TYPE_TEXT_EXTEND:
			input.setPadding(left, top, right, bottom);
			break;
		case TYPE_SELECT:
			select.setPadding(left, top, right, bottom);
			break;
		case TYPE_TIME:
			break;
		default:
			break;
		}
	}

	public InputView(Context context, AttributeSet attrs) {
		super(context, attrs);

		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.InputView);
		String text = a.getString(R.styleable.InputView_intitle);
		boolean readonly = a.getBoolean(R.styleable.InputView_readonly, false);
		int lines = a.getInt(R.styleable.InputView_lines, 1);
		// if (text == null) {
		// throw new RuntimeException("请设置标题");
		// }
		inputType = a.getInt(R.styleable.InputView_input_type, 1);
		color = a.getInt(R.styleable.InputView_font, 0);
		int labelStyle = a.getResourceId(R.styleable.InputView_labelStyle,
				R.style.input_lable);
		int textStyle = a.getResourceId(R.styleable.InputView_textStyle,
				R.style.input_text);
		newActivityInput = a.getBoolean(R.styleable.InputView_newActivityInput,
				false);
		a.recycle();
		this.init(context, attrs, text, labelStyle, textStyle, readonly, lines);
	}

	private void init(Context context, AttributeSet attrs, String text,
			int labelStyle, int textStyle, boolean readonly, int lines) {
		
		this.setOrientation(LinearLayout.HORIZONTAL);
		
		lable = new TextView(context);
		if(color==1){
			text =text+ "<font color='red'><b>" + "*" + "</b></font>";
		}
		lable.setText(Html.fromHtml(text));
		if (!isInEditMode()) {
			lable.setTextAppearance(context, labelStyle);
		} else {
			lable.setTextColor(Color.BLACK);
			lable.setTextSize(14);
		}

		LayoutParams lableParams = null;
		LayoutParams inputParams = null;
		if (!newActivityInput) {
			lableParams = new LayoutParams(
					LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
			lableParams.weight = 1;

			inputParams = new LayoutParams(
					LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
//			inputParams.weight = 2;
		} else if (inputType == TYPE_TOGGLE) {
			lableParams = new LayoutParams(
					LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT);
			lableParams.weight = 1;

			inputParams = new LayoutParams(
					LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
		} else {
			lableParams = new LayoutParams(
					LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);

			inputParams = new LayoutParams(
					LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT);
			inputParams.weight = 1;
		}
		lableParams.leftMargin = 8;
		inputParams.leftMargin = 5;
		this.addView(lable, lableParams);

		switch (inputType) {
		case TYPE_TEXT:
			input = new CustomEditText(context, attrs, text,
					lines == 1 ? newActivityInput : false);
			input.setBackgroundResource(background);
			if (!isInEditMode()) {
				input.setTextAppearance(context, textStyle);
			}
			input.setLines(lines);
			this.addView(input, inputParams);
			break;
		case TYPE_TEXT_EXTEND:
//			input = new CustomEditText(context, attrs, text,
//					lines == 1 ? newActivityInput : false, true);
//			input.setBackgroundResource(background);
//			if (!isInEditMode()) {
//				input.setTextAppearance(context, textStyle);
//			}
//			input.setLines(lines);
//			this.addView(input, inputParams);
//
//			LayoutParams extendParams = new LayoutParams(
//					BaseActivity.dip2px(context, 23),
//					LayoutParams.MATCH_PARENT);
//			extendParams.setMargins(0, 5, 4, 5);
//			extendButton = new Button(context);
//			extendButton.setText("");
//			extendButton.setBackgroundResource(R.drawable.yuyin);
//
//			TextView icon = new TextView(context);
//			icon.setBackgroundColor(0xffffffff);
//			Drawable rightCompoundDrawable = getResources().getDrawable(
//					R.drawable.icon_arrow);
//			icon.setCompoundDrawablesWithIntrinsicBounds(null, null,
//					rightCompoundDrawable, null);
//			LayoutParams iconParams = new LayoutParams(
//					LayoutParams.WRAP_CONTENT,
//					LayoutParams.MATCH_PARENT);
//			iconParams.setMargins(0, 0, 14, 0);
//
//			this.addView(extendButton, extendParams);
//			this.addView(icon, iconParams);
			
			break;
		case TYPE_SELECT:
			select = new SelectView(context, attrs, text, newActivityInput);
			select.setBackgroundResource(background);
			select.setTextAppearance(context, textStyle);
			this.addView(select, inputParams);
			break;
		case TYPE_TIME:
			break;
		case TYPE_TOGGLE:
			toggleButton = new ToggleButton(context);
			inputParams.width = LayoutParams.WRAP_CONTENT;
			this.addView(toggleButton, inputParams);
			break;
		default:
			break;
		}
		setEditAble(!readonly);
	}

	public void setText(String text) {
		switch (inputType) {
		case TYPE_TEXT:
			input.setText(text);
			break;
		case TYPE_TEXT_EXTEND:
			input.setText(text);
			break;
		case TYPE_SELECT:
			select.setSelectedValue(text);
			break;
		case TYPE_TIME:
			break;
		case TYPE_TOGGLE:
			toggleButton.setChecked("Y".equals(text));
			break;
		default:
			break;
		}
	}

	public void setVerify(int verify) {
		input.setVerify(verify);
	}

	/**
	 * 设置长度规则
	 * 例如:Length<=18
	 * @param verifys
	 */
	public void setVerifys(String verifys) {
		input.setVerifys(verifys);
	}
	
	public String getText() {
		switch (inputType) {
		case TYPE_TEXT:
			return input.getText().toString();
		case TYPE_TEXT_EXTEND:
			return input.getText().toString();
		case TYPE_SELECT:
			return select.getText().toString();
		case TYPE_TIME:
		case TYPE_TOGGLE:
			return toggleButton.isChecked() ? "Y" : "N";
		default:
			return "";
		}
	}

	public void setEditAble(boolean editAble) {
		switch (inputType) {
		case TYPE_TEXT:
			// if (editAble) {
			// input.setBackgroundResource(background);
			// } else {
			// input.setBackgroundDrawable(null);
			// }
			// input.setFocusable(editAble);
			// input.setFocusableInTouchMode(editAble);
			// input.setCursorVisible(editAble);
			// input.setClickable(editAble);
			input.setEditAble(editAble);
			break;
		case TYPE_TEXT_EXTEND:
			input.setEditAble(editAble);
			extendButton.setEnabled(editAble);
			break;
		case TYPE_SELECT:
			select.setSelectAble(editAble);
			break;
		case TYPE_TIME:
			break;
		case TYPE_TOGGLE:
			toggleButton.setEnabled(editAble);
			// toggleButton.setClickable(editAble);
			// toggleButton.setFocusable(editAble);
			// toggleButton.setFocusableInTouchMode(editAble);
			break;
		default:
			break;
		}
	}

	public boolean validate() {
		if (this.getVisibility() != View.VISIBLE) {
			return true;
		}
		switch (inputType) {
		case TYPE_TEXT:
			boolean verify = input.validate();
			this.verifyError = input.getVerifyError();
			return verify;
		case TYPE_TEXT_EXTEND:
			this.verifyError = input.getVerifyError();
			return input.validate();
		case TYPE_SELECT:
			return select.isSelected();
		case TYPE_TIME:
		default:
			return true;
		}
	}

	public void setOnSelectedListener(SelectView.OnSelectedListener selectedListener) {
		if (inputType == TYPE_SELECT) {
			select.setOnSelectedListener(selectedListener);
		}
	}

	public void setOnCheckedChangeListener(
			CompoundButton.OnCheckedChangeListener listener) {
		if (inputType == TYPE_TOGGLE) {
			toggleButton.setOnCheckedChangeListener(listener);
		}
	}
	
	/**
	 * 扩展按钮监听
	 * @param listener
	 */
	public void setOnExtendClickListener(
			OnClickListener listener) {
		this.extendButton.setOnClickListener(listener);
	}

	public void addTextChangedListener(TextWatcher watcher) {
		if (inputType == TYPE_TEXT || inputType == TYPE_TEXT_EXTEND) {
			input.addTextChangedListener(watcher);
		}
	}

	public void setLableText(String text) {
		lable.setText(Html.fromHtml(text));
	}

	public String getLableText() {
		return lable.getText().toString();
	}

	public CustomEditText getEditText() {
		return input;
	}

	public SelectView getSelectView() {
		return select;
	}


	public ToggleButton getToggleButton() {
		return toggleButton;
	}

	public String getVerifyError() {
		if (verifyError == null || "".equals(verifyError)) {
			verifyError = "请选择" + lable.getText().toString();
		}
		return verifyError;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (inputType) {
		case TYPE_TEXT:
			input.onTouchEvent(event);
			break;
//		case TYPE_TEXT_EXTEND:
//			input.onTouchEvent(event);
//			break;
		case TYPE_SELECT:
			select.onTouchEvent(event);
			break;
		case TYPE_TIME:
			break;
		case TYPE_TOGGLE:
			// toggleButton.toggle();
			break;
		default:
		}
		return initFlag ? true : super.onTouchEvent(event);
	}

	@Override
	public void setOnClickListener(OnClickListener l) {
		if (inputType == TYPE_TEXT || inputType == TYPE_TEXT_EXTEND) {
			input.setOnClickListener(l);
		}
		super.setOnClickListener(l);
	}

	/**
	 * 设置位置<br>
	 * 0：一个<br>
	 * 1：上<br>
	 * 2：中<br>
	 * 3：下
	 * 
	 * @param position
	 */
	public void setPosition(int position) {
		switch (position) {
		case 0:
			setBackgroundResource(R.drawable.input_panel_nor);
			break;
		case 1:
			setBackgroundResource(R.drawable.input_above_nor);
			break;
		case 2:
			setBackgroundResource(R.drawable.input_mid_nor);
			break;
		case 3:
			setBackgroundResource(R.drawable.input_below_nor);
			break;
		default:
		}
		// this.setPadding(0, 0, 0, 0);
	}

}
